//
//  AppDelegate.h
//  FleaFinder
//
//  Created by Hieu Le on 4/6/13.
//  Copyright (c) 2013 Kiwiweb. All rights reserved.
//

#import <UIKit/UIKit.h>
//#import <sqlite3.h>
#import <dispatch/dispatch.h>
#import "MarketType.h"
#import "Category.h"
#import "Common.h"

@class MarkedSummaryListViewController;
@class MarkedTypeViewController;
@class MarkedInfo;
//@class OpenMarket;
@class Map;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    
    NSDate *today;
    
    NSCalendar *calendar;
    
    NSMutableArray *marketTypeList;
    NSMutableArray *categoryList;
    NSMutableArray *marketList;
    NSMutableArray *businessMarketList;
    NSMutableArray *marketListWithLoadedLocation;
    
    //this variable is for storing data in structure
    // The KEY is MarketType and VALUE is a NSMutableArray of Markets
    //
    // dataOrganized
    //      |
    //      |-- Market Type
    //      |       |
    //      |       |-- Category
    //      |       |       |
    //      |       |       |-- Market
    //      |       |       |-- Market
    //      |       |
    //      |       |-- Category
    //      |       |       |
    //      |       |       |-- Market
    //      |       |       |-- Market
    NSMutableDictionary *dataOrganized;
    
    //Database vairables
    //NSString *databaseName;
    //NSString *databasePath;
    
    Common *common;
    
    //Threads
    dispatch_queue_t queue, queueTopMarkeder, queueGetLocation, queueReadMoreMarket;
    
    //this indicates the Market List is being loaded from website.
    BOOL isLoadingMarketList;
    BOOL isHasSomeMarketsLoaded;
    
    BOOL isLoadingBusinessMarketList;
    BOOL isLoadingLocation;
    
    BOOL isLoadingMoreMarketList;
    BOOL isLoadingMoreBusinessMarketList;
    
    MarketType* currentSelectedMarketType;
    Category *currentSelectedCategory;
    
    NSDateFormatter *dateFormatter;
    
    //int marketListPageSize;
    
    //load ui screens from storyboard
    UIStoryboard *storyBoard;
    MarkedSummaryListViewController *marketSummaryListViewController;
    MarkedInfo* markedInfoViewController;
    Map* mapViewController;
//    OpenMarket* openMarketViewController;
    MarkedTypeViewController* markedTypeViewController;
    
    NSDate* lastTimeLoadData;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic) NSMutableArray* marketTypeList;
@property (nonatomic) NSMutableArray* categoryList;

@property (nonatomic) NSMutableArray* marketList;
@property (nonatomic) NSMutableArray* businessMarketList;

@property (nonatomic) NSMutableArray* marketListWithLoadedLocation;

@property (nonatomic) Common* common;

@property (nonatomic) MarketType* currentSelectedMarketType;
@property (nonatomic) Category* currentSelectedCategory;

@property (nonatomic) NSMutableDictionary *dataOrganized;

@property (nonatomic) BOOL isLoadingMarketList;
@property (nonatomic) BOOL isHasSomeMarketsLoaded;
@property (nonatomic) BOOL isLoadingBusinessMarketList;
@property (nonatomic) BOOL isLoadingLocation;
@property (nonatomic) BOOL isLoadingMoreMarketList;
@property (nonatomic) BOOL isLoadingMoreBusinessMarketList;

@property (nonatomic) UIStoryboard* storyBoard;
@property (nonatomic) MarkedSummaryListViewController* marketSummaryListViewController;
@property (nonatomic) MarkedInfo* markedInfoViewController;
@property (nonatomic) Map* mapViewController;
//@property (nonatomic) OpenMarket* openMarketViewController;
@property (nonatomic) MarkedTypeViewController* markedTypeViewController;

@property (nonatomic) NSDate* lastTimeLoadData;

-(void) readMarketTypeListFromWebsite;

-(void) readCategoryListFromWebsite;

-(void) readMarketListFromWebsiteWithPageSize: (int) pageSize andPageNum:(int)pagenum;

-(void) readTopMarketListFromWebsiteWithPageSize: (int) pageSize andPageNum:(int)pagenum;

-(void) startReadMoreMarketListFromWebsite: (id)dataUser;

-(void) getMarketLocations;

-(void) formatDateOfMarket:(Marked*)marked;

-(void) addMarketsToMarketListInSortedManner: (NSMutableArray*)marketsToAdd toList:(NSMutableArray*)destArray;
-(void) addMarketToMarketListInSortedManner: (Marked*)market toList:(NSMutableArray*)destArray;

@end
